<template>
  <div>
    <el-container>
      <el-header>
        <h2>电商后台管理小实训~byc-0.1版本</h2>
        <div class="line"></div>
        <el-menu
          class="el-menu-demo"
          mode="horizontal"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          @select="handleSelect"
        >
          <el-menu-item
            :index="index.toString()"
            v-for="(item, index) in menus"
            :key="index"
            >{{ item.name }}</el-menu-item
          >
          <el-submenu index="2">
            <template slot="title">{{ this.$store.state.name }}</template>
            <el-menu-item index="2-1">修改</el-menu-item>
            <el-menu-item index="logout">退出</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside id="aside" :width="isCollapse ? '64px' : '250px'">
          <div class="bycopen" @click="bycgb">|||</div>
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            :collapse="isCollapse"
            router
          >
            <el-menu-item
              v-for="(item, index) in submenus"
              :key="index"
              :index="item.url"
            >
              <el-icon :class="item.icon"></el-icon>
              <span slot="title">{{ item.name }}</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main v-loading="$store.state.isLoading">
          <mbx :bread="bread"></mbx>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import mbx from "./common/mbx";
import nav from "../utils/navMenu";
export default {
  // 组件名称
  name: "demo",
  // 组件参数 接收来自父组件的数据
  props: [],
  // 局部注册的组件
  components: {
    mbx,
  },
  // 组件状态值
  data() {
    return {
      activeIndex2: 0,
      isCollapse: false,
      menus: [],
      submenus: [],
      bread: [],
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {
    $route: {
      handler(index) {
        this.bread = index.meta.bread;
      },
    },
  },
  // 组件方法
  methods: {
    bycgb() {
      this.isCollapse = !this.isCollapse;
    },
    // 退出登录
    handleSelect(index) {
      this.submenus = this.menus[index].children1;
      if (index == "logout") {
        this.$axios
          .post("/api/logout")
          .then((res) => {
            this.$store.commit("removetoken");
            this.$message.success("退出成功");
            this.$router.push("/login");
          })
          .catch((error) => {
            this.$message.error(error.response.data.msg);
            return false;
          });
      }
    }, //设置左侧栏的高度 ，充满屏幕
    setHeight() {
      //计算出浏览器可视化的高度
      let html = document.documentElement || document.body;
      let wh = html.clientHeight;
      //aside的高度=wh-60
      aside.style.height = wh - 70 + "px";
    },
  },
  //生命周期
  created() {
    console.log(nav);
    this.menus = nav;
    this.submenus = this.menus[0].children1;
  },
  mounted() {
    this.setHeight();
  },
};
</script> 

<style scoped lang="scss">
.el-aside {
  transition: width 1s;
}
.el-menu {
  border: none;
}
.bycopen {
  width: 100%;
  text-align: center;
  background: darkgray;
  font-size: 22px;
  color: ghostwhite;
  cursor: pointer;
  padding: 5px;
}
.el-main{
  padding:10px;
}
body {
  margin: 0;
  padding: 0;
}
.el-header {
  height: 70px;
  background: #545c64;
  color: azure;
  display: flex;
  align-items: center;
  display: flex;
  justify-content: space-between;
}
.el-aside {
  background: #545c64;
}
i,
span {
  font-size: 18px;
}
</style>

